// 单选:选择 上曲线

import React, { Component } from 'react';
import { Form, Select, message } from 'antd';
import './modal.css';
import PropTypes from 'prop-types';
import { reqProcessCurveList } from '../../api';
import { connect } from 'react-redux';

const { Option } = Select;

class SelectCurveUpForm extends Component {
  
  static propTypes = {
    setForm: PropTypes.func.isRequired,
    setCurve: PropTypes.func.isRequired,
    setDoc: PropTypes.func.isRequired, 

  };

  formRef = React.createRef();      

  state = {
    value: undefined,
    curvesList:[],  // 选中文件，获取曲线集合  
  };

  getAllCurves = async() => {
    if(this.props.docData.doc_id){
      const doc_id = this.props.docData.doc_id;
      const doc_type = this.props.docData.doc_type;
      const drill_id = this.props.docData.drill_id;
      const org_id = this.props.docData.org_id;
      const ws_id = this.props.docData.ws_id;
      const result = await reqProcessCurveList(doc_id,doc_type,drill_id,org_id,ws_id);   // 获取曲线列表
      if(result.code === 200){
        const curvesList = result.data;
        console.log("获取上曲线",curvesList)
        this.setState({ curvesList });   // 将获取到的所有曲线信息存在state中
      }else{
        message.error(result.data);
      } 
    }

  }


  onChange = value => {  // value：最后选中的曲线id 
    this.setState({ value });
    var curveInfo;
    this.state.curvesList.map((item) => {
      if(item.curve_id === value){
        curveInfo = item;
      }
    })
    
    this.props.setCurve(curveInfo);  
    this.props.setDoc({doc_id: this.props.docData.doc_id , doc_name: this.props.docData.title, doc_type: this.props.docData.doc_type })    
    //console.log("最终选择的曲线id",value,this.curveName,this.props.docData.doc_id,this.props.docData,this.props.docData.doc_type);
  };

  componentWillMount(){
    this.getAllCurves();
  }
  
  componentDidMount() {
    this.props.setForm(this.formRef); 
  }

  render() {
    const { curvesList } = this.state;
    return (
      <Form 
        ref={this.formRef}   
        name="control-ref" 
        onFinish={this.onFinish}  
      >    
        <Form.Item 
          name="curve_id" 
          label="选择曲线" 
          rules={[{ required: true }]}
        >
          <Select style={{ width: '100%' }} placeholder="请选择" onChange={this.onChange}>
            {curvesList.map((item) => <Option value={item.curve_id}>{item.curve_name}</Option>)}
          </Select>
        </Form.Item>
      </Form>
    );
  }
}

export default connect(
  state => ({user: state.user, docData: state.docData,}),
  {}
)(SelectCurveUpForm);